# -*- coding: utf-8 -*-
import sys
sys.path.append('../..')
from urllib.parse import urlencode
from common.config import *
from db.mysql import MysqlClient
from itertools import product
from common.helper import Helper
import requests
import json



class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "女性关爱特定疾病险"

    def genProductInfo(self):
        plan = {
            "planA": {
                # 年龄段
                "year": ["16-25", "26-35", "36-45", "46-55", "56-65"],
                # 性别
                "gender": ["F"],
                # 保障期限
                "expire": ["1年"],
                # 交费类型
                "payType": ["年交"],
                # 乳腺癌保赔
                "breaseCancer": ["50000"],
                # 女性特定疾病
                "womenSpeDisease": ["25000"],
                # 女性特定原位癌
                "womenGenCancer": ["5000"],
                # 女性特定疾病手术保障
                "womenSpeOperation": ["10000"],
                # 女性特定整容
                "womenSpeTidy": ["10000"]
            },
            "planB": {
                # 年龄段
                "year": ["16-25", "26-35", "36-45", "46-55", "56-65"],
                # 性别
                "gender": ["F"],
                # 保障期限
                "expire": ["1年"],
                # 交费类型
                "payType": ["年交"],
                # 乳腺癌保赔
                "breaseCancer": ["100000"],
                # 女性特定疾病
                "womenSpeDisease": ["50000"],
                # 女性特定原位癌
                "womenGenCancer": ["10000"],
                # 女性特定疾病手术保障
                "womenSpeOperation": ["10000"],
                # 女性特定整容
                "womenSpeTidy": ["10000"]
            }
        }
        for k in plan:
            for v in product(plan[k]["year"], plan[k]["gender"], plan[k]["expire"], plan[k]["payType"], plan[k]["breaseCancer"], plan[k]["womenSpeDisease"], plan[k]["womenGenCancer"], plan[k]["womenSpeOperation"], plan[k]["womenSpeTidy"]):
                data = Helper.formatProductInfo(plan[k], v)
                self.mysql.cursor.execute(
                    domain[self.name]["insert_sql"].format(**data))
                self.mysql._conn.commit()
                print(domain[self.name]["insert_sql"].format(**data))
    # 执行爬虫

    def requestJson(self, dt=None, mysql=None):
        print( dt )
        if dt["breaseCancer"] == "50000":
            data = {
                'requestData': '{"productId":"1222","isMulti":"1","issueChannel":"1","list":[{"cInsrncCde":"1200109","nSelectFlag":"1","nPrm":"0","nAmt":"5万"},{"cInsrncCde":"1200108","nSelectFlag":"1","nPrm":"32","nAmt":"2.5万"},{"cInsrncCde":"1200110","nSelectFlag":"1","nPrm":"0","nAmt":"5000"},{"cInsrncCde":"1200111","nSelectFlag":"1","nPrm":"10","nAmt":"1万"},{"cInsrncCde":"1200112","nSelectFlag":"1","nPrm":"4","nAmt":"1万"}],"productPlanId":"84bb77cd-8ed6-4e1f-9fae-fa347307736e","securityPeriod":"Y#1","ageRange":"'+dt["year"]+'"}'
            }
        else:
            data = {
                'requestData': ' {"productId":"1222","isMulti":"1","issueChannel":"1","list":[{"cInsrncCde":"1200109","nSelectFlag":"1","nPrm":"0","nAmt":"10万"},{"cInsrncCde":"1200108","nSelectFlag":"1","nPrm":"0","nAmt":"5万"},{"cInsrncCde":"1200110","nSelectFlag":"1","nPrm":"0","nAmt":"1万"},{"cInsrncCde":"1200111","nSelectFlag":"1","nPrm":"0","nAmt":"1万"},{"cInsrncCde":"1200112","nSelectFlag":"1","nPrm":"0","nAmt":"1万"}],"productPlanId":"a05aa0fd-a93d-47a7-9f2b-7bf34ace6e91","securityPeriod":"Y#1","ageRange":"'+dt["year"]+'"}'
            }

        payload = urlencode(data).replace("+", "")
        r = requests.post(
            url=domain[self.name]["url"], headers=domain[self.name]["header"], data=payload)
        if r.status_code == 200 and "premium" in r.text:
            price = Helper.decimalParse(r.json()["premium"])
            data = {
                "name": self.name,
                "id": dt["id"],
                "price": price
            }
            print(data)
            mysql.updatePrice(data)
        else:
            data = {
                "name": self.name,
                "id": dt["id"],
                "state": 3

            }
            mysql.updateTask(data)
            print("\n/*产品价格更新失败:%d*/" % dt["id"])
    def main(self):
        Helper.runProcess(mysql=self.mysql,name=self.name,func=self.requestJson )
